package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalOrdersMediaSta;
import com.fau.om.util.DateUtil;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  媒体订单数据统计Action
 * @author hufq
 * @datetime 2014年9月28日,下午12:56:37
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class MediaStatisticsAction extends PublicAction implements ModelDriven<MedicalOrdersMediaSta>{
	
	private static final long serialVersionUID = 3041465185097724887L;
	
	private MedicalOrdersMediaSta mediaSta = new MedicalOrdersMediaSta();

	/**
	 * @desc 媒体订单查询列表
	 * @return
	 * @author hufq
	 * @datetime 2014年9月28日,上午11:23:23
	 */
	@Action(value="media_statistics_init",results={@Result(name=SUCCESS,location="media_statistics_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询媒体订单统计
	 * @return null
	 * @author kangyj
	 * @datetime 2014年12月11日,下午5:32:00
	 */
	@Action(value="media_statistics_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getStatisticsManagerService().findMediaStatisticsByPage(mediaSta, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出媒体订单统计
	 * @return null
	 * @author liy
	 * @datetime 2014年12月14日,下午8:59:27
	 */
	@Action(value = "media_statistics_expots")
	public String exportsCenterCalls() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体订单统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsManagerService().mediaStaExport(this.mediaSta);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrdersMediaSta getModel() {
		return mediaSta;
	}

}
